package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.FpShopUserBackgroundDTO;
import com.financing.wap.service.IFpShopUserBackgroundService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("fpShopUserBackgroundController")
@RequestMapping("/api/fpShopUserBackground")
public class FpShopUserBackgroundController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpShopUserBackgroundService fpShopUserBackgroundService;
	
	@RequestMapping("/listFpShopUserBackground")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpShopUserBackgroundDTO fpShopUserBackground,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpShopUserBackground list begin .......");
			
			
			int count = fpShopUserBackgroundService.queryListCount(fpShopUserBackground);
			
			pager.setTotalCount(count);
			
			List<FpShopUserBackgroundDTO> fpShopUserBackgroundList = null;
			
			if(count > 0){
				fpShopUserBackgroundList = fpShopUserBackgroundService.queryList(fpShopUserBackground);
			}else{
				fpShopUserBackgroundList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpShopUserBackgroundList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpShopUserBackground list end .......");
		}catch(Exception e){
			e.printStackTrace();
			log.error("query fpShopUserBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addFpShopUserBackground")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpShopUserBackgroundDTO fpShopUserBackground){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpShopUserBackground  begin .......");
			
			DTOdateSetUtil.setCreateInfo(fpShopUserBackground);
			DTOdateSetUtil.setUpdateInfo(fpShopUserBackground);
			
			int id = fpShopUserBackgroundService.insert(fpShopUserBackground);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpShopUserBackground list end .......");
		}catch(Exception e){
			log.error("add fpShopUserBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpShopUserBackground")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpShopUserBackgroundDTO fpShopUserBackground){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpShopUserBackground  begin .......");
			
			Integer id = fpShopUserBackground.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpShopUserBackgroundDTO dto = fpShopUserBackgroundService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpShopUserBackground  end .......");
		}catch(Exception e){
			log.error("query detail fpShopUserBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpShopUserBackground")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpShopUserBackgroundDTO fpShopUserBackground){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpShopUserBackground  begin .......");
			
			Integer id = fpShopUserBackground.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			fpShopUserBackgroundService.delete(id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpShopUserBackground  end .......");
		}catch(Exception e){
			log.error("delete fpShopUserBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpShopUserBackground")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpShopUserBackgroundDTO fpShopUserBackground){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpShopUserBackground  begin .......");
			
			Integer id = fpShopUserBackground.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(fpShopUserBackground);
			
			fpShopUserBackgroundService.update(fpShopUserBackground);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpShopUserBackground list end .......");
		}catch(Exception e){
			log.error("update fpShopUserBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFpShopUserBackground")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpShopUserBackgroundDTO fpShopUserBackground){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpShopUserBackground  begin .......");
			
			Integer id = fpShopUserBackground.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(fpShopUserBackground);
			
			fpShopUserBackgroundService.updateForce(fpShopUserBackground);
			
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpShopUserBackground list end .......");
		}catch(Exception e){
			log.error("updateForce fpShopUserBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
